﻿using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;

namespace Dapper.ExpressionExtension.Resolvers
{
    /// <summary>
    /// 默认的表名解析器
    /// </summary>
    public class DefaultTableNameResolver : ITableNameResolver
    {
        /// <summary>
        /// 解析表名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public virtual string ResolveTableName(Type type)
        {
            var typeInfo = type.GetTypeInfo();
            var tableAttr = typeInfo.GetCustomAttribute<TableAttribute>();
            if (tableAttr == null)
            {
                return type.Name;
            }
            return string.IsNullOrEmpty(tableAttr.Schema) ? tableAttr.Name : $"{tableAttr.Schema}.{tableAttr.Name}";
        }
    }
}
